/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.tech15.pub.service;

import java.util.ArrayList;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;

import com.tech15.core.model.SysUser;
import com.tech15.pub.dao.PubGroupDao;
import com.tech15.pub.dao.PubGroupUserDao;
import com.tech15.pub.model.PubGroup;
import com.tech15.pub.model.PubGroupUser;
import com.tech15.pub.vo.query.PubGroupQuery;
import com.tech15.pub.vo.query.PubGroupUserQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class PubGroupManager extends BaseManager<PubGroup,Long>{

	private PubGroupDao pubGroupDao;
	private PubGroupUserDao pubGroupUserDao;
	
	public PubGroupUserDao getPubGroupUserDao() {
		return pubGroupUserDao;
	}
	public void setPubGroupUserDao(PubGroupUserDao pubGroupUserDao) {
		this.pubGroupUserDao = pubGroupUserDao;
	}
	public void setPubGroupDao(PubGroupDao dao) {
		this.pubGroupDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.pubGroupDao;
	}
	
	@Transactional(readOnly=true)
	public List<PubGroup> selectPubGroup() {
		return pubGroupDao.selectPubGroup();
	}
	@Transactional(readOnly=true)
	public List<PubGroup> selectPubGroupByUserid(Long userid) {
		return pubGroupDao.selectPubGroupByUserid(userid);
	}
	
	@Transactional(readOnly=true)
	public Page findPage(PubGroupQuery query) {
		return pubGroupDao.findPage(query);
	}
	
	public List<PubGroup> findAll(Integer appid){
		return pubGroupDao.findAll(appid);
	}
	public PubGroup getById(Long id){
		PubGroup pg = pubGroupDao.getById(id);
		return pg;
	}
	
	/** 插入数据 */
	public void save(PubGroup pg) throws DataAccessException{
		getEntityDao().save(pg);
		getEntityDao().flush();
		if(pg.getSelectUserIds() != null && !"".equals(pg.getSelectUserIds())){
			String selectuser = pg.getSelectUserIds().replace(" ","");
			String[] selectuserids = selectuser.split(",");
			List<PubGroupUser> list = new ArrayList<PubGroupUser>();
			for(int i=0;i<selectuserids.length;i++){
				PubGroupUser pgu = new PubGroupUser();
				pgu.setUserid(new Long(selectuserids[i]));
				pgu.setPgid(pg.getPgid());
				list.add(pgu);
			}
			getPubGroupUserDao().getHibernateTemplate().saveOrUpdateAll(list);
		}
	}
	
	/** 更新数据 */
	public void update(PubGroup pg) throws DataAccessException{
		getEntityDao().update(pg);
		if(pg.getSelectUserIds() != null && !"".equals(pg.getSelectUserIds())){
			getPubGroupUserDao().delete("delete pub_Group_user a where a.pgid="+pg.getPgid()+" and a.userid not in ("+pg.getSelectUserIds()+")");
			String selectuser = pg.getSelectUserIds().replace(" ","");
			String[] selectuserids = selectuser.split(",");
			List<PubGroupUser> list = new ArrayList<PubGroupUser>();
			for(int i=0;i<selectuserids.length;i++){
				PubGroupUser pgu = new PubGroupUser();
				pgu.setUserid(new Long(selectuserids[i]));
				pgu.setPgid(pg.getPgid());
				boolean res = pubGroupUserDao.isUnique(pgu, "pgid,userid");
				if(res){
					list.add(pgu);
				}
			}
			getPubGroupUserDao().getHibernateTemplate().saveOrUpdateAll(list);
		}else{
			getPubGroupUserDao().delete("delete pub_Group_user a where a.pgid="+pg.getPgid());
		}
	}
	public Page searchPubGroupUsers(PubGroupUserQuery query) {
		return pubGroupDao.searchPubGroupUsers(query);
	}
	@Transactional(readOnly=true)
	public List<PubGroupUser> selectPubGroupUsersByPgid(Long pgid) {
		return pubGroupDao.selectPubGroupUsersByPgid(pgid);
	}
	public List<PubGroup> selectGroup(Long userid) {
		return pubGroupDao.selectGroup(userid);
	}
	public List<SysUser> selectUser(Long keyid, String realname) {
		return pubGroupDao.selectUser(keyid,realname);
	}
	
}
